/**
 * 
 */
package commons.base.http.example;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import commons.base.util.SignUtil;

/**
 * @author yuan<cihang.yuan@happyelements.com>
 * @since 1.7
 */
public class AppServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static final String appSecret = "b6debd406c39fd868075ce555228a994";

	@Override
    protected void doGet( HttpServletRequest request,
                          HttpServletResponse response ) throws ServletException,
                                                        IOException
    {
    	System.err.println("====" + this);
    	printHeaders(request);
    	
    	System.err.println("sign = " + SignUtil.validSign(request, appSecret));
    	System.err.println(request.getQueryString());
        
    	response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println("<h1>Hello from HelloServlet</h1>");
    }
	
	@Override
	protected void doPost( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException,
	IOException
	{
		System.err.println("====" + this);
		printHeaders(request);
		
		System.err.println("sign = " + SignUtil.validSign(request, appSecret));
		System.err.println(request.getQueryString());
		
		response.setContentType("text/html");
		response.setStatus(HttpServletResponse.SC_OK);
		response.getWriter().println("<h1>Hello from HelloServlet</h1>");
	}
	
	private void printHeaders(HttpServletRequest request){
		Enumeration<String> e = request.getHeaderNames();
		while(e.hasMoreElements()){
			String name = e.nextElement();
			System.err.println(name + ":" + request.getHeader(name));
		}
	}
	
}
